import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import datasource.*;
public class WordCutter {
	private String sourceFileName = "";
	private BufferedReader reader = null;
	private WordIdentifier identifier = new WordIdentifier();
	private int currentLine = 0;
	
	public WordCutter(String name){
		sourceFileName = name;
		File f = new File(sourceFileName);
		try {
			reader = new BufferedReader(new FileReader(f));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Token getNextToken(){
		char c;
		String tempStr = "";
		try {
			while((c = (char)reader.read()) != -1)
			{
				if(!WordIdentifier.isChar(c)){
					if(c == 10)
						currentLine++;
					continue;
				}
				if(WordIdentifier.isLetter(c)){
					tempStr += c;
					reader.mark(1);
					while((c = (char)reader.read()) != -1){
						if(WordIdentifier.isLetter(c)||WordIdentifier.isNumber(c)){
							reader.mark(1);
							tempStr += c;
						}else{
							reader.reset();
							Token token = new Token(tempStr,TokenType.KEY_WORD,currentLine,ErrorType.NONE_ERROR);
							if(!identifier.isKeyWord(tempStr))
								token.setType(TokenType.VARIABLE_WORD);
							return token;
						}
					}
				}
				if(WordIdentifier.isNumber(c)){
					boolean hasLetter = false;
					tempStr += c;
					reader.mark(1);
					while((c = (char)reader.read()) != -1){
						if(WordIdentifier.isNumber(c)){
							reader.mark(1);
							tempStr += c;
							continue;
						}
						if(WordIdentifier.isLetter(c)){
							reader.mark(1);
							tempStr += c;
							hasLetter = true;
							continue;
						}
						break;
					}
					reader.reset();
					if(hasLetter)
						return new Token(tempStr,TokenType.ERROR,currentLine,ErrorType.WRONG_VARIABLE_NAME_ERROR);
					if(tempStr.startsWith("0"))
						return new Token(tempStr,TokenType.ERROR,currentLine,ErrorType.WRONG_NUMBER_ERROR);
					return new Token(tempStr,TokenType.NUMBER_VALUE_WORD,currentLine,ErrorType.NONE_ERROR);
				}
				if(identifier.isDelimiterWord(""+c)){
					tempStr += c;
					return new Token(tempStr,TokenType.DELIMITER_WORD,currentLine,ErrorType.NONE_ERROR);
				}
				if(identifier.isOperatorWord(""+c)){
					tempStr += c;
					if(c != '=')
						return new Token(tempStr,TokenType.OPERATOR_WORD,currentLine,ErrorType.NONE_ERROR);
					reader.mark(1);
					if((c = (char)reader.read()) == '='){
						tempStr += c;
						return new Token(tempStr,TokenType.COMPARISON_WORD,currentLine,ErrorType.NONE_ERROR);
					}
					reader.reset();
					return new Token(tempStr,TokenType.OPERATOR_WORD,currentLine,ErrorType.NONE_ERROR);
				}
				if(identifier.isComparisonWord(""+c)||c == '!'){
					tempStr += c;
					reader.mark(1);
					if((c = (char)reader.read()) == '='){
						tempStr += c;
						return new Token(tempStr,TokenType.COMPARISON_WORD,currentLine,ErrorType.NONE_ERROR);
					}
					reader.reset();
					if(c == '!')
						return new Token(tempStr,TokenType.ERROR,currentLine,ErrorType.WRONG_VARIABLE_NAME_ERROR);
					return new Token(tempStr,TokenType.COMPARISON_WORD,currentLine,ErrorType.NONE_ERROR);
				}
				if(c == '"'){
					while((c = (char)reader.read()) != '"')
						tempStr += c;
					return new Token(tempStr,TokenType.STRING_VALUE_WORD,currentLine,ErrorType.NONE_ERROR);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
